// featurePackage/pages/partnerArea/parnerArea.js
import {
  queryCategoryProductList
} from "../../../service/jb_product_service"
import {
  BIND_USER_ID
} from "../../../service/base_service"
import {
  loginWxAction
} from "../../../service/config"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navBarHeight: app.globalData.menuButton.top + app.globalData.menuButton.height,
    dataList: [

    ],
    requestParams: {
      keyword: "",
      priceOrder: '',
      salesOrder: "",
      news: 0,
      page: 1,
      limit: 20,
      cid: 0,
      isPartner: true,//是合伙人商品
      // isPersonCustom: false,//是否是私人定制商品

    },
    refreshFlag: false,
    isShowLoading: false,
    isBottomShow: false,
    isShareEnter: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    if (options.isShare == 1) {
      this.setData({
        isShareEnter: true,
      })
      if (options.userId) {
        wx.setStorageSync(BIND_USER_ID, options.userId);
      }
      this.userWxLogin();
    } else {
      this.queryCategoryProductList('refresh');
    }

    // 绑定分享参数
    wx.onCopyUrl(() => {
      return { query: `userId=${app.globalData.userId}&isShare=1` }
    })

  },
  onHide() {
    wx.offCopyUrl()
  },
  userWxLogin() {
    loginWxAction().then((res => {
      console.log("JJLog:登录返回:" + JSON.stringify(res));
      let data = res.data;
      if (res.code == 200) {
        app.globalData.nickName = data.nickname || "";
        app.globalData.avatar = data.avatar || "";
        app.globalData.phone = data.phone || "";
        app.globalData.isLogin = data.isLogin;
        app.globalData.userId = data.uid;
        this.queryCategoryProductList('refresh');
      }
    })).catch(err => {
      console.error("JJLog:userWxLogin", err);

    })
  },
  //跳转到搜索
  pushToSearch() {
    wx.navigateTo({
      url: '/featurePackage/pages/searchResult/searchResult?isPartner=true',
    })
  },

  simpleSortChange(e) {
    console.log("JJLog:哈哈哈哈:" + JSON.stringify(e));
    let detail = e.detail;
    switch (detail.key) {
      case 'all':
        this.setData({
          'requestParams.priceOrder': '',
          'requestParams.salesOrder': '',
          'requestParams.news': 0,
          'requestParams.page': 1,
          isBottomShow: false,
        })
        break;
      case 'price':
        this.setData({
          'requestParams.priceOrder': detail.sortType,
          'requestParams.salesOrder': '',
          'requestParams.news': 0,
          'requestParams.page': 1,
          isBottomShow: false,
        })
        break;
      case 'sale':
        this.setData({
          'requestParams.priceOrder': '',
          'requestParams.salesOrder': detail.sortType,
          'requestParams.news': 0,
          'requestParams.page': 1,
          isBottomShow: false,
        })
        break;
      case 'new':
        this.setData({
          'requestParams.priceOrder': '',
          'requestParams.salesOrder': '',
          'requestParams.news': 1,
          'requestParams.page': 1,
          isBottomShow: false,
        })
        break;

      default:
        break;
    }
    this.queryCategoryProductList(true, 'refresh');
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  queryCategoryProductList(loading, type) {
    wx.showLoading();
    queryCategoryProductList(this.data.requestParams).then(res => {
      wx.hideLoading()
      if (res.code == 200 && res.data) {
        let arr = (type == 'refresh' ? [] : this.data.dataList || []);
        arr = arr.concat(res.data.list || [])
        this.setData({
          dataList: arr,
          refreshFlag: false,
          isShowLoading: false,
          isBottomShow: res.data.totalPage == this.data.requestParams.page ? true : false,
        })
      }
    }).catch(err => {
      wx.hideLoading()
      wx.showToast({
        title: err || '加载失败',
        icon: "none"
      })
      if (this.data.requestParams.page != 1) {
        this.setData({
          'requestParams.page': this.data.requestParams.page - 1
        })
      }
      this.setData({
        refreshFlag: false,
      })
      console.error("JJLog:queryCategoryProductList", err);
    })
  },

  // 下拉刷新
  handleRefresh() {
    // TODO
    this.setData({
      'requestParams.page': 1,
      isBottomShow: false,
      refreshFlag: true,
    })
    this.queryCategoryProductList('refresh');
  },
  // 加载更多
  handleLoadmore() {
    console.log('加载更多');
    if (this.data.isBottomShow) {
      return;
    }
    this.setData({
      isShowLoading: true,
      'requestParams.page': this.data.requestParams.page + 1
    })
    this.queryCategoryProductList()
  },

  pushToPage(e) {
    let type = e.currentTarget.dataset.type;
    let navUrl = ''
    if (type == 'rank') {
      navUrl = "/featurePackage/pages/partnerRank/partnerRank"
    } else {
      navUrl = '/featurePackage/pages/partnerRule/partnerRule'
    }
    wx.navigateTo({
      url: navUrl,
    })
  },



  backHomeAction() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let userId = app.globalData.userId || ""
    let path = "/featurePackage/pages/partnerArea/parnerArea?isShare=1" + "&userId=" + userId
    return {
      title: app.globalData.commonShareTitle,
      path: path
    }
  }
})